// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2020, LomboTech Co.Ltd.
 * Author: lomboswer <lomboswer@lombotech.com>
 *
 * Voltage regulation driver for LomboTech
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 */

#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/regulator/driver.h>
#include <linux/regulator/machine.h>
#include <linux/regmap.h>
#include "regulator.h"

struct regulator_ops lombo_dcdc_regulator_ops = {
	.list_voltage		= regulator_list_voltage_linear,
	.get_voltage_sel	= regulator_get_voltage_sel_regmap,
	.set_voltage_sel	= regulator_set_voltage_sel_regmap,
	.enable			= regulator_enable_regmap,
	.disable		= regulator_disable_regmap,
	.is_enabled		= regulator_is_enabled_regmap,
};

struct regulator_ops lombo_ldo_regulator_ops = {
	.enable			= regulator_enable_regmap,
	.disable		= regulator_disable_regmap,
	.is_enabled		= regulator_is_enabled_regmap,
};

struct regulator_ops lombo_dummy_regulator_ops;

#define MAX_REGULATORS	32
static struct regulator_dev *regdev[MAX_REGULATORS];

static int lombo_regulator_probe(struct platform_device *pdev)
{
	int i, ret;
	int num_regulators;
	struct regulator_desc *lombo_regulators;
	struct regulator_dev *rd;
	struct regmap *regmap;
	struct regulator_config rc = {0};

	ret = __pmic_init(pdev);

	num_regulators = __get_regulators_count();
	lombo_regulators = __get_regulator_desc();

	regmap = devm_regmap_init(&pdev->dev, NULL, NULL, &pmic_regmap);
	if (IS_ERR(regmap)) {
		pr_err("%s: %d, initialize regmap for regulator failed\n",
			__func__, __LINE__);
		return PTR_ERR(regmap);
	}

	rc.dev = &pdev->dev;
	rc.regmap = regmap;

	for (i = 0; i < num_regulators; ++i) {
		rd = devm_regulator_register(&pdev->dev, &lombo_regulators[i],
			&rc);
		if (IS_ERR(rd)) {
			pr_err("failed to register regulator: %s\n",
				lombo_regulators[i].name);
			return  PTR_ERR(rd);
		}
		regdev[i] = rd;
	}

	return 0;
}

static int lombo_regulator_remove(struct platform_device *pdev)
{
	int i;
	int num_regulators;

	num_regulators = __get_regulators_count();

	for (i = 0; i < num_regulators; ++i)
		devm_regulator_unregister(&pdev->dev, regdev[i]);

	return 0;

}

static const struct of_device_id lombo_regulator_dt_ids[] = {
	{ .compatible = "lombo-regulator,ip6103", },
	{ .compatible = "lombo-regulator,discrete", },
	{}
};
MODULE_DEVICE_TABLE(of, lombo_regulator_dt_ids);

static struct platform_driver lombo_regulator_driver = {
	.driver = {
		.name = "lombo-regulator",
		.owner = THIS_MODULE,
		.of_match_table = lombo_regulator_dt_ids,
	},
	.probe = lombo_regulator_probe,
	.remove = lombo_regulator_remove,
};

static int __init regulator_driver_init(void)
{
	return platform_driver_register(&lombo_regulator_driver);
}
postcore_initcall(regulator_driver_init);

static void __exit regulator_driver_exit(void)
{
	platform_driver_unregister(&lombo_regulator_driver);
}
module_exit(regulator_driver_exit);
